package com.hushao.aimi.entity;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.UpdateTimestamp;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import javax.persistence.*;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.io.Serializable;
import java.sql.Timestamp;

/**
 * @description: 用户表
 * @author: 胡少
 * @create: 2018-06-03 15:33
 **/
@Entity
@Data
@ApiModel("用户基本信息")
public class AimiUser implements Serializable {

    @Id
    @GenericGenerator(name = "uuid",strategy = "uuid")
    @GeneratedValue(generator = "uuid")
    @Column(length = 32)
    private String id;


    @ApiModelProperty("用户名")
    @NotNull(message = "用户名不能为空")
    @Column(length = 64,nullable = false,unique = true)
    private String username;

    @ApiModelProperty("密码")
    @NotNull(message = "密码不能为空")
    @Column(length = 128,nullable = false)
    private String password;

    @ApiModelProperty("头像")
    @Column(length = 512)
    private String image;

    @ApiModelProperty("邮箱")
    @Email(message = "邮箱格式错误")
    @Column(length = 64,unique = true)
    private String email;

    @ApiModelProperty("手机号")
    @Pattern(regexp = "^[1](([3][0-9])|([4][5,7,9])|([5][^4,6,9])|([6][6])|([7][3,5,6,7,8])|([8][0-9])|([9][8,9]))[0-9]{8}$",message = "手机号码格式错误")
    @Column(length = 11,unique = true)
    private String phone;

    @ApiModelProperty("用户状态：0-禁用；1-正常[默认]")
    private byte status=1;

    @ApiModelProperty("创建时间")
    @CreationTimestamp
    private Timestamp createTime;

    @ApiModelProperty("更新时间")
    @UpdateTimestamp
    private Timestamp updateTime;

    @ApiModelProperty("备注")
    @Column(length = 512)
    private String comment;

    /**
     * 密码加密
     * @param password
     */
    public void setEncodePassword(String password) {
        PasswordEncoder encoder=new BCryptPasswordEncoder();
        String encoderPassowrd=encoder.encode(password);
        this.password = encoderPassowrd;
    }

    public AimiUser(String username, String password, String phone, String comment) {
        this.username = username;
        setEncodePassword(password);
        this.phone = phone;
        this.comment = comment;
    }

    public AimiUser(String username, String password, String image, String email,  String phone, byte status, Timestamp createTime, Timestamp updateTime, String comment) {
        this.username = username;
        setEncodePassword(password);
        this.image = image;
        this.email = email;
        this.phone = phone;
        this.status = status;
        this.createTime = createTime;
        this.updateTime = updateTime;
        this.comment = comment;
    }

}

